class mathfun {


	/**
	 * 加法精度计算
	 * @author 
	 * 
	 * @param {Object} number 数字
	 */

	calcadd(a, b) {
		var c, d, e;
		try {
			c = a.toString().split(".")[1].length
		} catch (f) {
			c = 0
		}
		try {
			d = b.toString().split(".")[1].length
		} catch (f) {
			d = 0
		}
		return e = Math.pow(10, Math.max(c, d)), (this.bcmul(a, e) + this.bcmul(b, e)) / e
	}

	/**
	 * 减法精度计算
	 * @author 
	 * 
	 * @param {Object} number 数字
	 */

	calcsub(a, b) {
		var c, d, e;
		try {
			c = a.toString().split(".")[1].length
		} catch (f) {
			c = 0
		}
		try {
			d = b.toString().split(".")[1].length
		} catch (f) {
			d = 0
		}
		return e = Math.pow(10, Math.max(c, d)), (this.bcmul(a, e) - this.bcmul(b, e)) / e
	}

	/**
	 * 乘法精度计算
	 * @author 
	 * 
	 * @param {Object} number 数字
	 */

	calcmul(a, b) {
		var c = 0,
			d = a.toString(),
			e = b.toString();
		try {
			c += d.split(".")[1].length
		} catch (f) {}
		try {
			c += e.split(".")[1].length
		} catch (f) {}
		return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c)
	}

	/**
	 * 除法精度计算
	 * @author 
	 * 
	 * @param {Object} number 数字
	 */

	calcdiv(a, b) {
		var c, d, e = 0,
			f = 0;
		try {
			e = a.toString().split(".")[1].length
		} catch (g) {}
		try {
			f = b.toString().split(".")[1].length
		} catch (g) {}
		return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), this.bcmul(c /
			d, Math.pow(10, f - e))
	}

	/**
	 * 保存聊天记录
	 * @author 
	 */
	setChat(data, send) {
		let uid = send == 'send' ? data.to_id : data.form.id;
		uni.getStorage({
			key: 'wanlchat:message_' + uid,
			success: function(res) {
				// 储存所有消息
				// let arr = res.data;
				// 优化,只储存 100条数据,超过100条,通过接口获取历史记录
				let arr = res.data.slice(-96);
				arr.push(data);
				uni.setStorageSync('wanlchat:message_' + uid, arr);
			},
			fail: function(res) {
				uni.setStorageSync('wanlchat:message_' + uid, [data]);
			}
		});
		// 返回原始数据
		return data;
	}

	//加法 
	accAdd: function(arg1, arg2) {
		var me = this;
		if (typeof arg1 === 'string') {
			arg1 = Number(arg1);
		}
		if (typeof arg2 === 'string') {
			arg2 = Number(arg2);
		}
		var r1, r2, m;
		try {
			r1 = arg1.toString().split(".")[1].length
		} catch (e) {
			r1 = 0
		}
		try {
			r2 = arg2.toString().split(".")[1].length
		} catch (e) {
			r2 = 0
		}
		m = Math.pow(10, Math.max(r1, r2))
		return me.keepDecimals((arg1 * m + arg2 * m) / m, 2);
	}
	//减法 
	floatSub: function(arg1, arg2) {
		var me = this;
		if (typeof arg1 === 'string') {
			arg1 = Number(arg1);
		}
		if (typeof arg2 === 'string') {
			arg2 = Number(arg2);
		}
		var r1, r2, m, n;
		try {
			r1 = arg1.toString().split(".")[1].length
		} catch (e) {
			r1 = 0
		}
		try {
			r2 = arg2.toString().split(".")[1].length
		} catch (e) {
			r2 = 0
		}
		m = Math.pow(10, Math.max(r1, r2));
		n = (r1 >= r2) ? r1 : r2;
		return me.keepDecimals(Ext.util.Format.round(((arg1 * m - arg2 * m) / m), n), 2);
	}
	// val要保留的数据，places保留位数，type返回类型（1数字，2字符串）
	keepDecimals: function(val, places, type) {
		if (typeof type === 'undefined') type = 1;
		var num = val + '';
		var numStart = num.split('.')[0];
		var numEnd = num.split('.')[1];
		var powVal = Math.pow(10, places);
		if (numEnd) {
			num = Math.round(num * powVal) / powVal + ''
			if (num.split('.')[1] && num.split('.')[1].length < places) {
				for (var n = 0; n < places - num.split('.')[1].length; n++) {
					num += '0'
				}
			}
		} else {
			num = numStart + '.'
			for (var n = 0; n < places; n++) {
				num += '0'
			}
		}
		return type == 1 ? num = num * 1 : num = num + ''
	}

	//乘法 
	accMul: function(arg1, arg2) {
		var me = this;
		if (typeof arg1 === 'string') {
			arg1 = Number(arg1);
		}
		if (typeof arg2 === 'string') {
			arg2 = Number(arg2);
		}
		var m = 0,
			s1 = arg1.toString(),
			s2 = arg2.toString();
		try {
			m += s1.split(".")[1].length
		} catch (e) {}
		try {
			m += s2.split(".")[1].length
		} catch (e) {}
		return me.keepDecimals(Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m), 2);
	}
	//除法
	accDiv: function(a, b) {
		var me = this;
		if (typeof a === 'string') {
			a = Number(a);
		}
		if (typeof b === 'string') {
			b = Number(b);
		}
		var c, d, e = 0,
			f = 0;
		try {
			e = a.toString().split(".")[1].length;
		} catch (g) {}
		try {
			f = b.toString().split(".")[1].length;
		} catch (g) {}
		return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), me
			.keepDecimals(me.accMul(c / d, Math.pow(10, f - e)), 2);
	}



}
export default new mathfun();

//使用方法
